<?php

namespace App\Http\Service\api;

use App\Models\User;
use App\Models\UserNotice;
use App\Models\UserNoticeSee;

class MessageService
{
//站内信服务层

    //用户站内信发送
    /**
     * @param $data //所需参数
     * @return array|string
     * @throws \Exception
     */
    public static function UserMessageAdd($data)
    {
        if (empty($data['uid'])||empty($data['type'])||empty($data['user'])||empty($data['title'])||empty($data['content'])
            ||empty($data['send_time'])){
            return ['code'=>501,'msg'=>'参数异常'];
        }
        $user_send_data = User::where('uid',$data['user'])->first();
        if (!$user_send_data){
            return ['code'=>502,'msg'=>'发送用户不存在'];
        }
        $user_receive_data = User::where('uid',$data['uid'])->first();
        if (!$user_receive_data){
            return  ['code'=>502,'msg'=>'接受用户不存在'];
        }
        $data['add_time'] = time();
//        return $data;
        try {
            $true_data =  UserNotice::create($data);
            if (!$true_data){
                throw new \Exception('用户站内信发送失败',504);
            }
            $true_see =UserNoticeSee::create([
                'nid'=>$user_send_data->uid,
                'uid'=>$user_receive_data->uid,
                'add_time'=>$data['send_time']
            ]);
            if (!$true_see){
                throw new \Exception('用户站内信记录生成失败',504);
            }
            return ['code'=>200,'msg'=>'用户站内信发送成功'];
        }catch (\Exception $exception){
            return $exception->getMessage();
//            return ['code'=>504,'msg'=>'用户站内信发送失败'];
        }
    }


    /**
     * @param $uid //查询接收用户id
     * @return array  //返回数组
     */
    public static function  UserMessageSelect($uid)
    {
        if (empty($uid)){
            return ['code'=>501,'msg'=>'参数错误'];
        }
        $user_letter = UserNotice::where('uid',$uid)->where('is_send',1)->get();
        if(empty($user_letter)){
            return  ['code'=>502,'msg'=>'没有新的信件'];
        }
        foreach ($user_letter as $k=>$v){
            $user_send_data = User::where('uid',$v['user'])->first();
            $user_send_name = $user_send_data->nickname;
            $v['send_name'] = $user_send_name;
        }
        return ['code'=>200,'msg'=>'返回数据','data'=>$user_letter];
    }


    /**
     * 删除站内信
     * @param $id
     * @return array
     */
    public static function UserMessageDel($id){
        $notice_data = UserNotice::where('id',$id)->first();
        if (empty($notice_data)){
            return ['code'=>501,'msg'=>'参数错误不存在'];
        }
        $is_send = $notice_data->is_send;
        if ($is_send==0){
            return ['code'=>504,'msg'=>'文件错误'];
        }
        $true_data = UserNotice::where('id',$id)->delete();
        if (!$true_data){
            return ['code'=>502,'msg'=>'删除失败'];
        }
        return ['code'=>200,'msg'=>'删除成功'];
    }

    /**
     * 调度任务执行 自动查询更改状态时间
     */
    public static function MessageList()
    {
        UserNotice::where('send_time','<',time())->update(['is_send'=>1]);
    }
}
